package com.example.common.mvvm

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.example.common.base.BaseActivity
import com.example.common.event.BaseEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 基础MVvM模式下的Activity
 * */

abstract class BaseMvvmActivity <T : ViewDataBinding>: BaseActivity<T>(){


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         initData()
        setDataObserver()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveEvent(event: BaseEvent){
        if (event!=null && event.target== this::class.java.simpleName){
            handEvent(event)
        }
    }

    open fun handEvent(event: BaseEvent){

    }

    open fun initData(){

    }

    open fun setDataObserver(){

    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }




}